package com.itheima.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.excption.MyException;
import com.itheima.service.UmsMenuApi;
import com.itheima.pojo.UmsMenu;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.UmsMenuNode;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UmsMenuService {
    @DubboReference
    private UmsMenuApi umsMenuApi;

    /**
     * 添加后台菜单
     * @param umsMenu
     */
    public void create(UmsMenu umsMenu) {
        boolean flag = umsMenuApi.create(umsMenu);
        if(flag==false){
            throw new MyException("菜单名或前端名重复无法添加");
        }
    }

    /**
     * 根据ID删除后台菜单
     * @param id
     */
    public void deleteById(Integer id) {
        //需要判断菜单下是否有子菜单
        //查询所有parentId,如果parentId中有id则代表有子类菜单不能删除
        List<Integer> parentIds=umsMenuApi.findParendId();
        for (Integer parentId : parentIds) {
            if(parentId==id){
                throw new MyException("当前菜单有子菜单无法删除");
            }
        }
        umsMenuApi.delete(id);
    }

    /**
     * 根据parentId分页查询后台菜单
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageResultVo findMenuByParentId(Integer parentId, Integer pageNum, Integer pageSize) {
        IPage<UmsMenu> iPage=umsMenuApi.findMenuByParentId(parentId,pageNum,pageSize);
        List<UmsMenu> umsMenus = iPage.getRecords();
        PageResultVo resultVo=new PageResultVo();
        resultVo.setPageNum(pageNum);
        resultVo.setPageSize(pageSize);
        resultVo.setTotal(Convert.toInt(iPage.getTotal()));
        resultVo.setTotalPages(Convert.toInt(iPage.getPages()));
        resultVo.setList(umsMenus);
        return resultVo;
    }

    /**
     * 修改后台菜单
     * @param id
     * @param umsMenu
     * @return
     */
    public void editById(Integer id, UmsMenu umsMenu) {
        //通过id判断当前和传过来的是否相同
        UmsMenu menu = umsMenuApi.findOneById(id);
        if(menu.getTitle().equals(umsMenu.getTitle())){
            umsMenuApi.editById(id,umsMenu);
            return;
        }
        boolean flag = umsMenuApi.isExists(umsMenu);
        if(!flag){
            throw new MyException("菜单名重复无法修改");
        }else {
            umsMenuApi.editById(id,umsMenu);
        }

    }

    /**
     * 修改菜单显示状态
     * @param hidden
     * @param id
     * @return
     */
    public void editHidden(Integer id, Integer hidden) {
        umsMenuApi.editHidden(id,hidden);
    }

    /**
     * 根据ID获取菜单详情
     * @param id
     * @return
     */
    public UmsMenu findOneById(Integer id) {
        return umsMenuApi.findOneById(id);
    }

    /**
     * 树形结构返回所有菜单列表
     * @return
     */
    public List<UmsMenuNode> treeList() {
        //先获取所有一级菜单  4个
        List<UmsMenu> umsMenusFirstList=umsMenuApi.findFirstAll();
        List<UmsMenu> umsMenuSecondList=new ArrayList<>();
        List<UmsMenuNode> children=new ArrayList<>();

        //遍历
        for (UmsMenu umsMenu : umsMenusFirstList) {
            Long id = umsMenu.getId();
            //获取所有二级菜单
            umsMenuSecondList=umsMenuApi.findSecondAll(id);
            UmsMenuNode umsMenuNode=new UmsMenuNode();
            BeanUtils.copyProperties(umsMenu,umsMenuNode);
            children.add(umsMenuNode);
            umsMenuNode.setChildren(umsMenuSecondList);
        }
        return children;
    }
}